<template>
  <div id="apps">
    <h1>📝 待办事项 TodoList</h1>

    <div class="input-area">
      <input v-model="newTodo" @keyup.enter="addTodo" placeholder="输入新的待办事项" />
      <button @click="addTodo">添加</button>
    </div>

    <div class="filters">
      <button @click="filter = 'all'" :class="{ active: filter === 'all' }">全部</button>
      <button @click="filter = 'active'" :class="{ active: filter === 'active' }">未完成</button>
      <button @click="filter = 'completed'" :class="{ active: filter === 'completed' }">已完成</button>
    </div>

    <p v-if="filteredTodos.length === 0" class="empty-tip">这里什么都没有呢～快去添加吧！(｡･ω･｡)</p>

    <transition-group name="list" tag="ul" class="todo-list">
      <li v-for="(todo, index) in filteredTodos" :key="todo.id">
        <input type="checkbox" v-model="todo.done" @change="saveTodos" />
        <span :class="{ done: todo.done }">{{ todo.text }}</span>
        <button class="delete-btn" @click="deleteTodo(todo.id)">删除</button>
      </li>
    </transition-group>

  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'

// 数据
const newTodo = ref('')
const todos = ref([])
const filter = ref('all')  // 筛选状态

// localStorage 键名
const STORAGE_KEY = 'vue-todolist'

// 从 localStorage 加载数据
function loadTodos() {
  const storedTodos = localStorage.getItem(STORAGE_KEY)
  if (storedTodos) {
    todos.value = JSON.parse(storedTodos)
  }
}

// 保存数据到 localStorage
function saveTodos() {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(todos.value))
}

// 添加任务
function addTodo() {
  const text = newTodo.value.trim()
  if (text) {
    todos.value.push({ id: Date.now(), text, done: false })
    newTodo.value = ''
    saveTodos()
  }
}

// 删除任务
function deleteTodo(id) {
  todos.value = todos.value.filter(todo => todo.id !== id)
  saveTodos()
}

// 清除已完成任务
function clearCompleted() {
  todos.value = todos.value.filter(todo => !todo.done)
  saveTodos()
}

// 计算属性：未完成数量
const remaining = computed(() => {
  return todos.value.filter(todo => !todo.done).length
})

// 计算属性：根据筛选状态过滤后的列表
const filteredTodos = computed(() => {
  if (filter.value === 'active') {
    return todos.value.filter(todo => !todo.done)
  } else if (filter.value === 'completed') {
    return todos.value.filter(todo => todo.done)
  }
  return todos.value
})

// 监听 todos 变化自动保存
watch(todos, () => {
  saveTodos()
}, { deep: true })

// 生命周期 mounted
onMounted(() => {
  loadTodos()
  console.log('(hash)路由模式TodoList已加载～ฅ^•ﻌ•^ฅ')
})
</script>

<style scoped>
/* 背景色和字体 */
body {
  margin: 0;
  background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%);
  font-family: 'Comic Sans MS', 'Chalkboard', cursive;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 中心卡片 */
#apps {
  max-width: 600px;  /* 这是合适的宽度 */
  background: white;
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);
  text-align: center;
  margin-top: 100px;
}

h1 {
  color: #ff6ec7;
  margin-bottom: 20px;
  font-weight: bold;
}

/* 输入区 */
.input-area {
  display: flex;
  margin-bottom: 20px;
}

.input-area input {
  flex: 1;
  padding: 10px;
  font-size: 16px;
  border: 2px solid #ffb6b9;
  border-radius: 8px;
  transition: border-color 0.3s;
}

.input-area input:focus {
  border-color: #ff6ec7;
  outline: none;
}

.input-area button {
  margin-left: 10px;
  padding: 10px 20px;
  background-color: #ff6ec7;
  color: white;
  font-weight: bold;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.input-area button:hover {
  background-color: #ff4fa3;
}

/* 筛选按钮 */
.filters {
  margin-bottom: 20px;
}

.filters button {
  margin: 0 5px;
  background: #f3d6ff;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s;
}

.filters button:hover {
  transform: translateY(-2px);
}

.filters button.active {
  background: #ff6ec7;
  color: white;
}

/* 列表 */
.todo-list {
  list-style: none;
  padding: 0;
}

.todo-list li {
  display: flex;
  align-items: center;
  background: #fafafa;
  margin-bottom: 10px;
  padding: 10px 15px;
  border-radius: 10px;
  transition: all 0.3s ease;
}

.todo-list li:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.todo-list input[type="checkbox"] {
  margin-right: 10px;
  cursor: pointer;
}

.todo-list span {
  flex: 1;
  text-align: left;
  word-break: break-word;
}

.todo-list span.done {
  text-decoration: line-through;
  color: #aaa;
}

/* 删除按钮 */
.delete-btn {
  background-color: #ff4d4f;
  color: white;
  border: none;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.delete-btn:hover {
  background-color: #d9363e;
}

/* 空提示 */
.empty-tip {
  color: #888;
  margin: 20px 0;
}

/* 底部 */
footer {
  margin-top: 20px;
  padding-top: 10px;
  border-top: 1px solid #eee;
}

.clear-btn {
  margin-top: 10px;
  background-color: #ffb6b9;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s;
}

.clear-btn:hover {
  background-color: #ff9a9e;
}

/* 动画效果 */
.list-enter-active, .list-leave-active {
  transition: all 0.4s;
}

.list-enter-from, .list-leave-to {
  opacity: 0;
  transform: translateY(20px);
}

@media (max-width: 640px) {
  #apps {
    margin: 20px;
    padding: 15px;
  }
  
  .input-area {
    flex-direction: column;
  }
  
  .input-area button {
    margin-left: 0;
    margin-top: 10px;
  }
}
:root {
  font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
  line-height: 1.5;
  font-weight: 400;

  color-scheme: light dark;
  color: rgba(255, 255, 255, 0.87);
  background-color: #242424;

  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a {
  font-weight: 500;
  color: #646cff;
  text-decoration: inherit;
}
a:hover {
  color: #535bf2;
}

body {
  margin: 0;
  display: flex;
  place-items: center;
  min-width: 320px;
  min-height: 100vh;
}

h1 {
  font-size: 3.2em;
  line-height: 1.1;
}

button {
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 0.6em 1.2em;
  font-size: 1em;
  font-weight: 500;
  font-family: inherit;
  background-color: #1a1a1a;
  cursor: pointer;
  transition: border-color 0.25s;
}
button:hover {
  border-color: #646cff;
}
button:focus,
button:focus-visible {
  outline: 4px auto -webkit-focus-ring-color;
}

.card {
  padding: 2em;
}

@media (prefers-color-scheme: light) {
  :root {
    color: #213547;
    background-color: #ffffff;
  }
  a:hover {
    color: #747bff;
  }
  button {
    background-color: #f9f9f9;
  }
}

</style>